package ch.dreamweb.readpractice.client.lesetraining.train.view;

import ch.dreamweb.readpractice.client.lesetraining.Constants;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public abstract class UnitViewImpl extends Composite implements UnitView {

	@UiField
	protected HorizontalPanel panelhead;

	@UiField
	protected Button buttonMenu;

	@UiField
	protected Label labelTimeUp;

	@UiField
	protected Label labelInfoLevel;

	@UiField
	protected HorizontalPanel panelFootprints;

	protected Presenter presenter;

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@UiHandler("buttonMenu")
	void onButtonMenuClick(ClickEvent e) {
		if (presenter != null) {
			presenter.onMenuButtonClicked();
		}
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void init() {
		labelTimeUp.setText("Die Zeit ist abgelaufen.");
		buttonMenu.setText("Zum Menu");
		setMenuVisible(false);
		setInfo("");

		getButtonStart().setText("Start");
		getButtonStart().setEnabled(true);
		getButtonStart().setFocus(true);
		getButtonStart().setTabIndex(0);

		// getButtonOk().setText("OK");
		// getButtonOk().setEnabled(false);
		// getButtonOk().setFocus(false);
		// getButtonOk().setTabIndex(1);
		// getButtonOk().setVisible(false);
	}

	@Override
	public void setMenuVisible(boolean visible) {
		labelTimeUp.setVisible(visible);
		buttonMenu.setVisible(visible);
	}

	@Override
	public void setFocusMenu() {
		buttonMenu.setFocus(true);
	}

	@Override
	public void setInfoLevel(String text) {
		labelInfoLevel.setText(text);
	}

	@Override
	public void setInfo(String text, String color) {
		getLabelInfo().setText(text);
		getLabelInfo().removeStyleName(
				color.equals(Constants.CORRECT) ? getStyle().incorrect()
						: getStyle().correct());
		getLabelInfo().addStyleName(
				color.equals(Constants.CORRECT) ? getStyle().correct()
						: getStyle().incorrect());
	}

	@Override
	public void setInfo(String text) {
		setInfo(text, Constants.CORRECT);
	}

	@Override
	public void showFootprints(int recordLevel, int actualLevel) {
		panelFootprints.clear();
		int max = 0;
		int i = 0;
		if (recordLevel > actualLevel) {
			max = recordLevel;
		} else {
			max = actualLevel;
		}
		// for (i = 1; i <= max; i++) {
		// if ((i <= recordLevel) && (i <= actualLevel)) {
		// panelFootprints.add(new Image("pictures/border_filled.gif"));
		// } else if ((i <= recordLevel) && (i > actualLevel)) {
		// panelFootprints.add(new Image("pictures/border.gif"));
		// } else {
		// panelFootprints.add(new Image("pictures/full.gif"));
		// }
		//
		// }
		for (i = 1; i <= 21; i++) {
			if (i > max) {
				panelFootprints.add(new Image("pictures/empty.gif"));
			} else {
				if ((i <= recordLevel) && (i <= actualLevel)) {
					panelFootprints
							.add(new Image("pictures/border_filled.gif"));
				} else if ((i <= recordLevel) && (i > actualLevel)) {
					panelFootprints.add(new Image("pictures/border.gif"));
				} else {
					panelFootprints.add(new Image("pictures/full.gif"));
				}
			}

		}
	}

	@Override
	public void showElephant(int type) {
		if (type == 0) {
			panelhead.clear();
		} else if (type == 2) {
			panelhead.clear();
			panelhead.add(new Image("pictures/achtung.png"));
		} else if (type == 3) {
			panelhead.clear();
			panelhead.add(new Image("pictures/rekord.png"));
		}
	}

}